import { NativeMessenger } from '@syt/nativemessenger'

let __instance = (function () {
  let instance;
  return (newInstance) => {
    if (newInstance) instance = newInstance;
    return instance;
  }
}())

export default class ErrorHanlder {
  static get share() {
    return new ErrorHanlder()
  }

  constructor() {
    if (__instance()) return __instance()

    __instance(this)
  }


  RegisteredHandler(vue) {
    // var handler = this
    // window.onerror = function (message, source, lineno, colno, error) {
    //   handler.OnWindowError(message, source, lineno, colno, error)
    // }
    // vue.config.errorHandler = function (err, vm, info) {
    //   handler.OnVueError(err, vm, info)
    // }
  }

  OnWindowError(message, source, lineno, colno, error) {
    try {
      NativeMessenger.share.sendToNative('onWindowError', {
        message,
        source,
        lineno,
        colno,
        error
      })
    } catch (err) {
      console.log(err)
    }
  }

  OnVueError(err, vm, info) {
    try {
      NativeMessenger.share.sendToNative('onVueError', {
        err,
        info
      })
    } catch (error) {
      console.log(error)
    }
  }
}